<template>
  <div>
    <el-descriptions title="用户信息" direction="vertical" :column="4" border>
      <el-descriptions-item label="用户名">{{form.nickname}}</el-descriptions-item>
      <el-descriptions-item label="手机号">{{form.phone}}</el-descriptions-item>
      <el-descriptions-item label="居住地" :span="2">{{form.address}}</el-descriptions-item>
      <el-descriptions-item label="头像(点击可换头像)">
        <el-upload
            class="avatar-uploader"
            :action="'http://' + serverIp +':9090/file/upload'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
        >
          <img v-if="form.avatarUrl" :src="form.avatarUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-descriptions-item>
      <el-descriptions-item label="注册时间">{{form.createTime}}</el-descriptions-item>
    </el-descriptions>

    <el-button type="text" @click="submitFormVisible = true">提交旅游中心资质审核</el-button>

    <el-dialog title="资质资料提交" :visible.sync="submitFormVisible" width="60%" >
      <el-form label-width="80px" size="small">
        <el-form-item label="证明文件图">
          <el-upload
              class="avatar-uploader"
              :action="'http://' + serverIp +':9090/file/upload'"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
          >
            <img v-if="file.pic" :src="file.pic" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="文件描述">
          <el-input v-model="file.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitFile">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import {serverIp} from "../../../public/config";

export default {
  name: "PersonalCenter",
  data() {
    return {
      serverIp: serverIp,
      submitFormVisible:false,
      form: {},
      file:{},
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    this.getUser().then(res => {
      console.log(res)
      this.form = res
    })
  },
  methods: {
    async getUser() {
      return (await this.request.get("/user/username/" + this.user.username)).data
    },
    save() {
      this.request.post("/user", this.form).then(res => {
        if (res.code === '200') {
          this.$message.success("保存成功")

          // 触发父级更新User的方法
          this.$emit("refreshUser")

          // 更新浏览器存储的用户信息
          this.getUser().then(res => {
            res.token = JSON.parse(localStorage.getItem("user")).token
            localStorage.setItem("user", JSON.stringify(res))
          })

        } else {
          this.$message.error("保存失败")
        }
      })
    },
    submitFile(){

    },
    handleAvatarSuccess(res) {
      this.form.avatarUrl = res
    }
  }
}
</script>

<style scoped>
.avatar-uploader {
  text-align: left;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>